package me.yuzho.study.concurrency.java8;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/** Callable 相比 Runnable 可以获取线程执行的返回值
 * @author yzhou-citrix
 *
 */
public class Case3Callable {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        
        Callable<Integer> task = new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {

                try {
                    TimeUnit.SECONDS.sleep(1);
                    return 123;
                }
                catch (InterruptedException e) {
                    throw new IllegalStateException("task interrupted", e);
                }
            }
        };
        
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<Integer> future = executor.submit(task);
        executor.shutdown();

        System.out.println("future done? " + future.isDone());

        Integer result = future.get();

        System.out.println("future done? " + future.isDone());
        System.out.print("result: " + result);

    }

}
